package com.tulingxueyuan.mall.modules.pms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.PmsProductCategoryDTO;
import com.tulingxueyuan.mall.dto.ProductCateChildrenDTO;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author Miao
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    PmsProductCategoryService productCategoryService;

    /*
    *  商品分类，获取
    *  url:'/productCategory/list/'+parentId,
    *  method:'get',
    *  parentId,params   :params----》pageNum、pageSize
    * */
    @RequestMapping(value = "/list/{parentId}",method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsProductCategory>> getList(@PathVariable Long parentId,
                                                                @RequestParam Integer pageNum,
                                                                @RequestParam(value = "pageSize",defaultValue = "5" ) Integer pageSize){
        /*获取商品分类表PmsProductCategory中的全部信息*/
        Page page=productCategoryService.list(parentId,pageNum,pageSize);

        return CommonResult.success(CommonPage.restPage(page));
    }

    /*
    商品分类-是否显示
    url:'/productCategory/update/showStatus',
        method:'post',
        data:data
            data.append('ids',ids);
            data.append('showStatus',row.showStatus);
    */
    @RequestMapping(value = "/update/showStatus",method = RequestMethod.POST)
    public CommonResult updateShowStatus(@RequestParam(value = "ids") List<Long> ids,
                                         @RequestParam(value = "showStatus") Integer showStatus){
        boolean result = productCategoryService.updateShowStatus(ids,showStatus);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /*
    状态栏
    url:'/productCategory/update/navStatus',
    method:'post',
    data:data
        data.append('ids',ids);
        data.append('navStatus',row.navStatus);
    */
    @RequestMapping(value = "/update/navStatus",method = RequestMethod.POST)
    public CommonResult updateNavStatus(@RequestParam(value = "ids") List<Long> ids,
                                        @RequestParam(value = "navStatus") Integer navStatus){
        boolean result = productCategoryService.updateNavStatus(ids,navStatus);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }

    }

    /*
    * 商品删除
    * export function deleteProductCate(id) {
          return request({
          url:'/productCategory/delete/'+id,
          method:'post'
        })
        }
    * */
    @RequestMapping(value = "/delete/{id}")
    public CommonResult delete(@PathVariable Long id){
        boolean result = productCategoryService.removeById(id);
        if (result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /*
    * 分类添加
    * url:'/productCategory/create',
    * method:'post',
    * data:data
    * */
    @RequestMapping(value = "/create",method=RequestMethod.POST)
    public CommonResult create(@RequestBody PmsProductCategoryDTO productCategoryDTO){

        boolean result=productCategoryService.CustomSave(productCategoryDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据Id获取分类信息
     * url:'/productCategory/'+id,
     * method:'get',
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public CommonResult<PmsProductCategory> getById(@PathVariable Long id){

        PmsProductCategory pmsProductCategory = productCategoryService.getById(id);
        return CommonResult.success(pmsProductCategory);
    }

    /**
     *  分类修改提交
     * Request URL: http://localhost:8099/productCategory/update/{id}
     * Request Method: POST
     *
     */
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult update(@RequestBody PmsProductCategoryDTO productCategoryDTO){

        boolean result=productCategoryService.saveOrUpdate(productCategoryDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     * 获取商品一级分类和二级分类的下拉级联数据
     * Request URL: http://localhost:8099/productCategory/list/withChildren
     * Request Method: GET
     */
    @RequestMapping(value = "/list/withChildren",method = RequestMethod.GET)
    public CommonResult getWithChildren(){
        List<ProductCateChildrenDTO> list=productCategoryService.getWithChildren();
        return CommonResult.success(list);
    }



}

